package org.tobuy.dao.proxy;

import java.util.ArrayList;
import java.util.List;

import org.tobuy.dao.IAuctionDAO;
import org.tobuy.dao.impl.AuctionDAOImpl;
import org.tobuy.dbc.DatabaseConnection;
import org.tobuy.vo.Auction;
import org.tobuy.vo.Deal;
import org.tobuy.vo.User;

public class AuctionDAOProxy implements IAuctionDAO {
	
	private DatabaseConnection dbc= null;
	private IAuctionDAO dao = null;
	
	public AuctionDAOProxy(){
		try{
			this.dbc = new DatabaseConnection();
		}catch(Exception e){
			e.printStackTrace();
		}
		this.dao = new AuctionDAOImpl(dbc.getConnection());
	}

	public List<Auction> findAuctions() throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		try{
			auctions = this.dao.findAuctions();
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return auctions;
	}

	public List<Auction> findBuyerAuctions(User user) throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		try{
			auctions = this.dao.findBuyerAuctions(user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return auctions;
	}

	public List<Auction> findSellerAuctions(User user) throws Exception {
		List<Auction> auctions = new ArrayList<Auction>();
		try{
			auctions = this.dao.findSellerAuctions(user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return auctions;
	}

	public boolean addAuction(Auction auction) throws Exception {
		boolean flag = false;
		try{
			flag = this.dao.addAuction(auction);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public boolean updateAuction(String item_id, double price, String buyer_id)
			throws Exception {
		boolean flag = false;
		try{
			flag = this.dao.updateAuction(item_id, price, buyer_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public boolean deleteAuction(String item_id) throws Exception {
		boolean flag = false;
		try{
			flag = this.dao.deleteAuction(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public Auction findAuction(String item_id) throws Exception {
		Auction auction = new Auction();
		try{
			auction = this.dao.findAuction(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return auction;
	}

}
